#!/bin/bash
if [ $# -lt 1 ]; then
    echo 'usage: genmrcc.sh -n 8 -m 20GB -p tddft -d b2gplyp -D "SOS-ADC(2)" -S 3 -T 2 -b def2-svp abc.xyz'
    echo 'option:-[n number of cores, default 16]'
    echo '       -[m memory, default 90% of system total mem, e.g. 8GB]'
    echo '       -[t template file, the last line should be geom=xyz]' 
    echo '       -[p profile, cc2,adc2,cisd,cisdi,tddft,tda and their sos-/scs- version]'
    echo '       -[b basis set, default: CC-pVDZ, avail: Dunning[-F12], Pople, Ahlrichs[D]] '
    echo '       -[d dft functionls, avail DH: B2PLYP,B2GPPLYP,DSDPBEP86,XYG3,SCAN0-2,dRPA75]' 
    echo '       -[D 2nd order excited part in double hybrid TDDFT, default: ADC(2)] '
    echo '       -[S number of singlets, default: 3]'
    echo '       -[T number of triplets, default: 2]'
    echo '       -[R redcost_exc option, default: off, avail: on,off,1-10, recom: 3 or 8]'
    echo '       -[L localcc option, default: off, avail: on,off,2013,2015,2016,2018, recom: off]'
    echo '       -[C cctol option, default: off]'
    echo '       -[s additional suffix for directory name]'
    exit 1
fi
while getopts ":n:m:t:p:b:s:d:D:S:T:L:R:C:" opt
do
    case $opt in
        n) NPROC=$OPTARG;;
        m) MEM=$OPTARG;;
        t) TEMPLATE=$OPTARG;;
        p) PROFILE=$OPTARG;;
        s) SUFFIX=$OPTARG;;
        b) BASIS=$OPTARG;;
        d) DFT=$OPTARG;;
        D) DHEXC=$OPTARG;;
        S) NSINGLETS=$OPTARG;;
        T) NTRIPLETS=$OPTARG;;
        L) LOCALCC=$OPTARG;;
        C) CCTOL=$OPTARG;;
        R) REDCOST=$OPTARG;;
        ?) echo 'usage: genmrcc.sh -n 8 -m 20GB -p tddft -d b2gplyp -S 3 -T 2 -b def2-svp abc.xyz'
           echo 'option:-[n number of cores, default 16]'
           echo '       -[m memory, default 90% of system total mem, e.g. 8GB]'
           echo '       -[t template file, the last line should be geom=xyz]' 
           echo '       -[p profile: cc2,"adc(2)","cis(d)","cis(di)" and their sos-/scs- version, tddft,tda ]'
           echo '       -[b basis set, default: CC-pVDZ, avail: Dunning[-F12], Pople, Ahlrichs[D]] '
           echo '       -[d dft functionls, avail DH: B2PLYP,B2GPPLYP,DSDPBEP86,XYG3]' 
           echo '       -[D 2nd order excited part in double hybrid TDDFT, default: ADC(2), avail: ADC(2),CIS(2) and their SCS-/SOC- version] '
           echo '       -[S number of singlets, default: 3]'
           echo '       -[T number of triplets, default: 2]'
           echo '       -[R redcost_exc option, default: off, avail: on,off,1-10, recom: 3 or 8]'
           echo '       -[L localcc option, default: off, avail: on,off,2013,2015,2016,2018, recom: off]'
           echo '       -[C cctol option, default: off]'
           echo '       -[s additional suffix for directory name]'
           exit 1;;
    esac
done
shift $(($OPTIND - 1))

dos2unix $1

function profile2temp {
pname=$(echo $PROFILE | tr -d '()"' | tr '[:lower:]' '[:upper:]')
echo "mem=8GB" > $pname.temp
job=$(echo $PROFILE | tr '[:lower:]' '[:upper:]' | tr -d '"')
echo "calc=$job" >> $pname.temp
echo "basis=CC-pVDZ" >> $pname.temp
echo "dft=off" >> $pname.temp
echo "dhexc=ADC(2)" >> $pname.temp
echo "nsing=3" >> $pname.temp
echo "ntrip=2" >> $pname.temp
echo "#itol=10" >> $pname.temp
echo "#scftol=8" >> $pname.temp
echo "#cctol=6" >> $pname.temp
echo "#scfdtol=9" >> $pname.temp
echo "localcc=off" >> $pname.temp
echo "lcorthr=Normal" >> $pname.temp
echo "redcost_exc=off" >> $pname.temp
echo "#ccprog=cis" >> $pname.temp
echo "verbosity=2" >> $pname.temp
echo "charge=0" >> $pname.temp
echo "mult=1" >> $pname.temp
echo "geom=xyz" >> $pname.temp
}


if [ -z "$NPROC" ]; then
    NPROC=16
else
    NPROC=$(expr $NPROC + 0)
fi
if [ -z "$MEM" ];then
    MEM=`free | awk '{if(NR==2)print $2/1024*0.9"MB"}'`
    echo "set mem to $MEM"
fi
if [ -n "$TEMPLATE" ];then
    if [[ -f "$TEMPLATE" ]];then
        echo "using template file $TEMPLATE"
        MIDNAME=${TEMPLATE%.*}
    else
        echo "Error!!! template file $TEMPLATE not exist"
        exit
    fi
elif [ -n "$PROFILE" ]; then
    echo "using profile $PROFILE"
    profile2temp
    TEMPLATE=${pname}.temp
    MIDNAME=${pname%DFT}
else
    echo "Error!!! you must set either a template file or a profile"
fi
if [ -n "$DFT" ] && [ "$DFT" != "off" ];then
    MIDNAME=${DFT}_${MIDNAME}
    if [ -n "$DHEXC" ];then
        dhexc_str=`echo $DHEXC | tr -d '()"-'`
        MIDNAME=${MIDNAME}_${dhexc_str}
    fi
fi

while [[ $# -ge 1 ]]
do
basename=${1%.*}_${MIDNAME}${SUFFIX}
mkdir ${basename} 2> /dev/null
cp $1 ${basename}
cp $TEMPLATE ${basename}
cd ${basename}
WD=`pwd`
cat $TEMPLATE $1 > MINP
if [ -n "$BASIS" ];then
    sed -i "/basis=/c\basis=$BASIS" MINP
fi
if [ -n "$NSINGLETS" ];then
    sed -i "/nsing=/c\nsing=$NSINGLETS" MINP
fi
if [ -n "$NTRIPLETS" ];then
    sed -i "/ntrip=/c\ntrip=$NTRIPLETS" MINP
fi
if [ -n "$DFT" ];then
    sed -i "/dft=/c\dft=$DFT" MINP
fi
if [ -n "$LOCALCC" ];then
    sed -i "/localcc=/c\localcc=$LOCALCC" MINP
fi
if [ -n "$REDCOST" ];then
    sed -i "/redcost_exc=/c\redcost_exc=$REDCOST" MINP
fi
if [ -n "$CCTOL" ];then
    sed -i "/cctol=/c\cctol=$CCTOL" MINP
fi
if [ -n "$DHEXC" ];then
    DHEXC=`echo $DHEXC | tr -d '"'`
    sed -i "/dhexc=/c\dhexc=$DHEXC" MINP
fi
sed -i "/mem=/c\mem=$MEM" MINP
echo "export OMP_NUM_THREADS=$NPROC" > runmrcc.sh
echo "export MKL_NUM_THREADS=$NPROC" >> runmrcc.sh
echo "export OMP_PLACES=cores" >> runmrcc.sh
echo "export OMP_PROC_BIND=spread,close" >> runmrcc.sh
echo "cd $PWD" >> runmrcc.sh
echo 'start=`date +%s`' >> runmrcc.sh
echo "dmrcc | tee runmrcc.out" >> runmrcc.sh
echo 'end=`date +%s`' >> runmrcc.sh
echo 'runtime=$(echo "$end - $start" | bc -l )' >> runmrcc.sh
echo "echo \$runtime > runtime" >> runmrcc.sh
echo "cd -" >> runmrcc.sh
echo "runmrcc.sh and MINP generated in directory $basename"
echo "\"sh ${basename}/runmrcc.sh\" to start calculation"
cd ..
shift 1
done 
